//
//  RCSelectorView.m
//  RocketProject
//
//  Created by chen jiedan on 13-6-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCSelectorView.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import <QuartzCore/QuartzCore.h>


#define kButtonBaseTag          10
#define kButtonUnderViewBaseTag 20
#define kClickIndexUnSelected   -1


@implementation RCSelectorView{
    id<RCSeletorViewDelegate> _delegate;
    TabBar                    *_tabBar;
    
    NSInteger                  _selectedIndex;
    NSInteger                  _totalCount;
}
@synthesize delegate = _delegate;
@synthesize selectedIndex = _selectedIndex;

- (id)initWithFrame:(CGRect)frame titleArray:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _totalCount = [titles count];
        _selectedIndex = kClickIndexUnSelected;
        _tabBar = [[TabBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) tabBarTitles:titles];
        [self addSubview:_tabBar];
        _tabBar.tabBarDelegate = self;
        [_tabBar release];
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void) didUnselectTabBar:(int)index
{
    if (index != kClickIndexUnSelected) {
        [self didSelectTabBar:index];
    }
}
- (void) didSelectTabBar:(int)clickIndex
{
    if (_selectedIndex != kClickIndexUnSelected) {
        NSInteger currentUnderViewTag = kButtonUnderViewBaseTag + _selectedIndex;
        UIView *sub = [self viewWithTag:currentUnderViewTag];
        if (sub != nil) {
            CGRect newFrame = self.frame;
            newFrame.size.height = newFrame.size.height - sub.frame.size.height;
            self.frame = newFrame;
            [sub removeFromSuperview];
        }
    }
    
    if (clickIndex == _selectedIndex) {
        //点击的是上次展开点击展开的按钮
        _selectedIndex = kClickIndexUnSelected;
    }else{
        UIView *subView = [_delegate viewForSelectorIndex:clickIndex];
        //底部加入一个透明的按钮，便于用户取消
        CGRect containerFrame = CGRectMake(0, self.frame.size.height, 320, UIScreenHeight-20-44-self.frame.size.height);
        UIButton *container = [[UIButton alloc] initWithFrame:containerFrame];
        [container addTarget:self action:@selector(selectorHide) forControlEvents:UIControlEventTouchUpInside];
        container.tag = kButtonUnderViewBaseTag + clickIndex;
        [container addSubview:subView];
        [container setBackgroundColor:[UIColor clearColor]];
        [self addSubview:container];
        [container release];
        
        CGRect newFrame = self.frame;
        newFrame.size.height = newFrame.size.height + container.frame.size.height;
        self.frame = newFrame;
        _selectedIndex = clickIndex;
    }
}


- (void) selectorHide
{
    [_tabBar clearState];
    [self didUnselectTabBar:_selectedIndex];
}

- (void) changeCurrentTabbarTitle:(NSString *)newTitle
{
    [_tabBar setTitleName:newTitle forTabIndex:_selectedIndex];
}
@end




#pragma 
#pragma mark ###SubView3 Tableview list###

@implementation RCSubViewTablelist{
    NSArray *_titles;
}
- (id) initWithFrame:(CGRect)frame andTitles:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        _titles = [titleArray retain];
        [self setUpViews];
    }
    return self;
}

- (void) setUpViews
{
    NSArray *subViews = [self subviews];
    for (UIView *subView in subViews) {
        [subView removeFromSuperview];
    }
    
    UITableView *t = [[UITableView alloc] initWithFrame:self.bounds];
    [self addSubview:t];
    t.delegate = self;
    t.dataSource = self;
    [t release];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_delegate respondsToSelector:@selector(rcSubViewTablelist:didSelectTitle:atIndex:)]) {
        [_delegate rcSubViewTablelist:self didSelectTitle:[_titles objectAtIndex:[indexPath row]] atIndex:[indexPath row]];
    }
}

//#define kBGImageFile             @"image_discout_headerButton_normal.png"

//NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kBGimageFile];
//UIImage *bgImage = [UIImage imageWithContentsOfFile:filePath];
//self.contentView.layer.contents = (id)bgImage.CGImage;

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease];
//    UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
//    cell.layer.contents = (id)bgImage.CGImage;
    
    cell.textLabel.text = [_titles objectAtIndex:[indexPath row]];
    [cell.textLabel setFont:[UIFont fontWithName:kFontNameHelvetica size:14]];
//    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
//    cell setBackgroundColor:[UIColor colorWithHex:0x]
    return cell;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titles count];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void) dealloc
{
    [_titles release];
    [super dealloc];
}
@end
 
